package kr.ac.kaist.idb.csnql.entity;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import kr.ac.kaist.cs.idb.csnql.utility.Base64Coder;

/*
 * @Class Name : Coordinate
 * @Programmer : Sooseok Park (sspark1988@gmail.com)
 * @Date :  2013.11
 * 
 * @Description-
 * Class for Coordinate information(x,y)
 */

public class Coordinate implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 953192894826280568L;
	private int x;
	private int y;
	
	public Coordinate(int x, int y){
		this.x = x;
		this.y = y;
	}
	
	public Coordinate(){
		
	}
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
	/**
	 * @brief Marshalls the Event into a string and encodes it using Base64 coding
	 * @return A string that is Base64 encoded containing the marshalled event
	 * @throws IOException
	 */
	public String marshall() throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(baos);
		out.writeObject(this);
		out.close();
		byte[] marshalledEvent = baos.toByteArray();
		String base64EndocdedMarshalledEvent = Base64Coder.encodeLines(marshalledEvent);
		return(base64EndocdedMarshalledEvent);
	}
	
	/**
	 * @brief Decodes the string from Base64 coding and unmarshalls the event and returns it
	 * @param base64EncodedMarshalledEvent A String that is encoded using Base64 coding and contains
	 *        a marshalled event
	 * @return The event object that was marshalled and encoded in the argument string 
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static Coordinate unmarshall(String base64EncodedMarshalledEvent)
	throws IOException, ClassNotFoundException {
		byte[] marshalledEvent = Base64Coder.decodeLines(base64EncodedMarshalledEvent);
		ObjectInputStream ois =
			new ObjectInputStream(new ByteArrayInputStream(marshalledEvent));
		Coordinate o = (Coordinate)ois.readObject();
		ois.close();
		return(o);
	}
}
